package com.justinwang4.androidservicedemo.services;

import android.app.IntentService;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    public void onCreate() {
        // 必须调用super以妥善处理工作线程的生命周期
        Log.i("Test", "onCreate has been called!");
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        // 不允许服务绑定
        return null;
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
//        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        Log.i("Test", "onStartCommand has been called!");
        // 必须调用super以妥善处理工作线程的生命周期
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onStart(@Nullable Intent intent, int startId) {
        // 在onStartCommand之后执行
        Log.i("Test", "onStart has been called!");
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        // 必须调用super以妥善处理工作线程的生命周期
        Log.i("Test", "onDestroy has been called!");
        super.onDestroy();
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.i("Test", "onHandleIntent has been called!");
        try {
            Thread.sleep(5000);
            Log.i("Test", "onHandleIntent ended!");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

}
